/**
 * 月安装量-汇总数据
 * wangmei
 */
import React, { Component } from 'react';
import { message  } from 'antd';
import { getMsoMonthChart } from '../../../Server/SystemManger';
import CommonDaySum from '../install/CommonDaySum';
import moment from 'moment';
import qs from 'qs';

import {REQUEST_SUCCESS,REQUEST_UNAUTHORIZED} from '../../../common/const';
require('../../systemManager/customer.css');

const dateFormat = 'YYYY-MM';
const currentDate = new Date();

class DaySum extends Component{
    constructor(props) {
        super(props);
        this.state = {
            data: [], //汇总数据表格数据
            loading: false,  //汇总数据加载状态
            xData: [], //汇总数据x轴数据
            yData: [], //汇总数据y轴数据
            titleDate: currentDate
        };
    }

    //查询汇总方法
    getDailyChart = (values) =>{
        this.setState({ loading: true });
        const param = {
            queryMonth: moment(values.queryMonth).format(dateFormat),
        };
        const _param = qs.stringify(param);
        getMsoMonthChart(_param).then(res => {
            if(res && res.code == REQUEST_SUCCESS){
                let data = res.data.points;
                let xData = [];
                let yData = [];
                for(let i = 0; i < data.length; i++){
                    xData.push(data[i].x);
                    yData.push(data[i].y);
                }
                this.setState({
                    data: data,
                    xData: xData,
                    yData: yData,
                    loading: false,
                    titleDate: values.queryMonth
                })
            }else {
                this.setState({
                    loading: false
                })
                res && res.code !== REQUEST_UNAUTHORIZED && message.error(res.msg);
            }
        });
    }

    render(){
        const { data, loading, xData, yData, titleDate } = this.state;
        let _data = [];
        for(var i=0;i<data.length;i++){
            let temp = {
                "分部名称": data[i].x,
                "MSO版本量": data[i].y,
            }
            _data.push(temp);
        }

        const columns = [
            {
                title: "分部名称",
                dataIndex: "x",
                align: 'center',
                width: '50%'
            }, {
                title: "MSO版本量",
                dataIndex: "y",
                align: 'center',
                width: '50%'
            }
        ];

        return(
            <CommonDaySum
                flag={'month'}
                columns={columns}
                data={data}
                loading={loading}
                xData={xData}
                yData={yData}
                xName="分部名称" 
                yName="MSO版本量" 
                title={"MSO版本信息（"+moment(titleDate).format('YYYY年MM月')+"）" }
                legendData={['MSO版本信息']}
                formatter={moment(titleDate).format('YYYY年MM月')+'<br />{a}<br />{b} : {c}'}
                _headers={['分部名称', 'MSO版本量' ]}
                _data={_data}
                fileName={'MSO版本信息'}
                getChartData={this.getDailyChart}
            />
        ) 
    }
}

export default DaySum